//
//  ChatToolMoreView.m
//  ChatTool
//
//  Created by zhuxuehang on 13-10-10.
//  Copyright (c) 2013年 zhuxuehang. All rights reserved.
//


#import "ChatToolMoreView.h"
#import "UIUtil.h"
#import "ISTThemeButton.h"
#import "ISTThemeLabel.h"
#import "MicroDef.h"
#import "RIButtonItem.h"
#import "UIActionSheet+Blocks.h"
#define IMAGESIZE [UIImage imageNamed:@"icon-message"].size
#define GAP1 20.0f
#define GAP2 (HARDWARE_SCREEN_WIDTH - IMAGESIZE.width * 4) / 5.0
@interface ChatToolMoreView ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, strong) NSMutableArray *titleArray;


@end

static ChatToolMoreView *shared;
@implementation ChatToolMoreView
@synthesize moreViewDelegate = _moreViewDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //        [CNMultiThemeManager registerForThemeChange:self];
        // Initialization code
    }
    return self;
}

- (NSMutableArray *)buttonArray
{
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (NSMutableArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

+(ChatToolMoreView*)defaultMoreView
{
    @synchronized(self)
    {
        if(shared == nil)
        {
            shared = [[ChatToolMoreView alloc] init];
        }
    }
    return shared;
}

-(id)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, HARDWARE_SCREEN_HEIGHT - 64, HARDWARE_SCREEN_WIDTH,200);
        [self setDesign];
    }
    return self;
}

-(void)setDesign
{
    
    UIScrollView *scr= [[UIScrollView alloc] init];
    scr.backgroundColor = ClearColor;
    //        self.bounces = NO;
    scr.scrollEnabled = YES;
    scr.pagingEnabled = YES;
    //        self.alwaysBounceHorizontal = NO;
    //        self.alwaysBounceVertical = NO;
    scr.showsHorizontalScrollIndicator = NO;
    scr.showsVerticalScrollIndicator = NO;
    scr.delegate = self;
    [self addSubview:scr];
    self.scrollView = scr;
    ISTThemeButton *pic = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height)
                                      normalImg:@"icon-img"
                                    selectedImg:@"icon-img"
                                            tag:100
                                         action:@selector(selectPicture)];
    ISTThemeLabel *picTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                   backgroundColor:[UIColor clearColor]
                                              text: @"图片"
                                          textFont: [UIFont systemFontOfSize:14]
                                         textColor:@""
                                      defaultColor:RGBCOLOR(146, 146, 146)
                                     textAlignment:NSTextAlignmentCenter];
    
    
    
    ISTThemeButton *camera = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height)
                                         normalImg:@"icon-camera"
                                       selectedImg:@"icon-camera" tag:101 action:@selector(takePhotoWithCamera)];
    ISTThemeLabel *cameraTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                      backgroundColor:[UIColor clearColor]
                                                 text: @"拍照"
                                             textFont: [UIFont systemFontOfSize:14]
                                            textColor:@""
                                         defaultColor:RGBCOLOR(146, 146, 146)
                                        textAlignment:NSTextAlignmentCenter];
    ISTThemeButton *smallVideo = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height)
                                             normalImg:@"video"
                                           selectedImg:@"video" tag:102 action:@selector(takeVideoWithCamera)];
    ISTThemeLabel *smallVideoTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                          backgroundColor:[UIColor clearColor]
                                                     text: @"微视频"
                                                 textFont: [UIFont systemFontOfSize:14]
                                                textColor:@""
                                             defaultColor:RGBCOLOR(146, 146, 146)
                                            textAlignment:NSTextAlignmentCenter];
    
    ISTThemeButton *file = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height)
                                       normalImg:@"icon-files"
                                     selectedImg:@"icon-files"
                                             tag:103
                                          action:@selector(selectFile)];
    ISTThemeLabel *fileTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                    backgroundColor:[UIColor clearColor]
                                               text: @"文件"
                                           textFont: [UIFont systemFontOfSize:14]
                                          textColor:@""
                                       defaultColor:RGBCOLOR(146, 146, 146)
                                      textAlignment:NSTextAlignmentCenter];
    
    ISTThemeButton *message = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height)
                                          normalImg:@"icon-message"
                                        selectedImg:@"icon-message"
                                                tag:104
                                             action:@selector(selectMessage)];
    self.message = message;
    ISTThemeLabel *messageTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                       backgroundColor:[UIColor clearColor]
                                                  text: @"短信"
                                              textFont: [UIFont systemFontOfSize:14]
                                             textColor:@""
                                          defaultColor:RGBCOLOR(146, 146, 146)
                                         textAlignment:NSTextAlignmentCenter];
    
    self.duanxin = messageTitle;
    ISTThemeButton *location = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height) normalImg:@"weizhi" selectedImg:@"weizhi" tag:105 action:@selector(selectLocation)];
    ISTThemeLabel *locationTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                        backgroundColor:[UIColor clearColor]
                                                   text: @"位置"
                                               textFont: [UIFont systemFontOfSize:14]
                                              textColor:@""
                                           defaultColor:RGBCOLOR(146, 146, 146)
                                          textAlignment:NSTextAlignmentCenter];
    ISTThemeButton *money = [self buttonWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, IMAGESIZE.height) normalImg:@"bribery-money" selectedImg:@"bribery-money" tag:102 action:@selector(selectMoney)];
    ISTThemeLabel *moneyTitle = [self labelWithFrame:CGRectMake( 35 ,37.5f, IMAGESIZE.width, 25)
                                     backgroundColor:[UIColor clearColor]
                                                text: @"红包"
                                            textFont: [UIFont systemFontOfSize:14]
                                           textColor:@""
                                        defaultColor:RGBCOLOR(146, 146, 146)
                                       textAlignment:NSTextAlignmentCenter];
    [self.buttonArray addObjectsFromArray:[NSArray arrayWithObjects:pic,camera,smallVideo, file, message, location,money,nil]];
    [self.titleArray addObjectsFromArray:[NSArray arrayWithObjects:picTitle, cameraTitle,smallVideoTitle, fileTitle, messageTitle,locationTitle,moneyTitle, nil]];
    [scr addSubview:pic];
    [scr addSubview:camera];
    [scr addSubview:smallVideo];
    [scr addSubview:file];
    [scr addSubview:message];
    [scr addSubview:location];
    [scr addSubview:money];
    [scr addSubview:picTitle];
    [scr addSubview:cameraTitle];
    [scr addSubview:smallVideoTitle];
    [scr addSubview:fileTitle];
    [scr addSubview:messageTitle];
    [scr addSubview:locationTitle];
    [scr addSubview:moneyTitle];
    self.scrollView.contentSize = CGSizeMake(0, 0);
    
}



- (ISTThemeButton *)buttonWithFrame:(CGRect)frame normalImg:(NSString*)normalImg selectedImg:(NSString*)selectedImg tag:(int)tagValue action:(SEL)sel
{
    ISTThemeButton *btn = [[ISTThemeButton alloc] initWithFrame:CGRectZero];
    [btn setFrame:frame];
    [btn setImageStr:normalImg forState:UIControlStateNormal];
    [btn setImageStr:selectedImg forState:UIControlStateSelected];
    [btn setImageStr:selectedImg forState:UIControlStateHighlighted];
    
    [btn setTag:tagValue];
    [btn addTarget:nil action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(ISTThemeLabel*)labelWithFrame:(CGRect)frame backgroundColor:(UIColor*)color text:(NSString*)text textFont:(UIFont*)textFont textColor:(NSString*)textColor defaultColor:(UIColor *)defaultColor textAlignment:(NSTextAlignment)textAlignment
{
    ISTThemeLabel* labe = [[ISTThemeLabel alloc] initWithFrame:frame];
    [labe setBackgroundColor:color];
    [labe setText:text];
    [labe setTextColor:textColor andDefaultColor:defaultColor];
    [labe setTextAlignment:textAlignment];
    [labe setFont:textFont];
    
    return labe;
}

- (void)selectPicture
{
    if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(selectPicFromAlbum)]) {
        [self.moreViewDelegate selectPicFromAlbum];
    }
}

- (void)takePhotoWithCamera
{
    if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(takePhotosWithCamera)]) {
        [self.moreViewDelegate takePhotosWithCamera];
    }
}

- (void)selectFile
{
    if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(didSelectFile)]) {
        [self.moreViewDelegate didSelectFile];
    }
}

- (void)selectMessage
{
    if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(didSelectMessage)]) {
        [self.moreViewDelegate didSelectMessage];
    }
    
}
-(void)selectLocation
{
    if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(didSelectLocation)]) {
        [self.moreViewDelegate didSelectLocation];
    }
    
}
-(void)selectMoney
{
    if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(didSelectMoney)]) {
        [self.moreViewDelegate didSelectMoney];
    }
    
}
-(void)takeVideoWithCamera
{
    RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"取消" action:^{
        
    }];
    
    __weak typeof(self) weakself = self;
    RIButtonItem *photoItem = [RIButtonItem itemWithLabel:@"录制" action:^{
        if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(didselectVideofromCamera)]) {
            [weakself.moreViewDelegate didselectVideofromCamera];
        }
        
    }];
    
    RIButtonItem *selectItem = [RIButtonItem itemWithLabel:@"从相册选择" action:^{
        if (_moreViewDelegate && [_moreViewDelegate respondsToSelector:@selector(didselectVideofromAlbum)]) {
            [weakself.moreViewDelegate didselectVideofromAlbum];
        }
    }];
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil cancelButtonItem:cancelItem destructiveButtonItem:selectItem otherButtonItems:photoItem, nil];
    [actionSheet showInView:self];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnX = 0;
    CGFloat btnY = GAP1;
    for (int i = 0; i < self.buttonArray.count; i ++) {
        UIButton *button = self.buttonArray[i];
        btnX = GAP2  + i * (IMAGESIZE.width + GAP2);
        if (i>=4) {
            btnX = GAP2  + (i-4) * (IMAGESIZE.width + GAP2);
            btnY = GAP1 + IMAGESIZE.height + 25 ;
        }
        button.frame = CGRectMake(btnX, btnY, IMAGESIZE.width, IMAGESIZE.height);
        UILabel *label = self.titleArray[i];
        label.frame = CGRectMake(btnX, btnY + IMAGESIZE.height, IMAGESIZE.width, 25);
    }
    
    self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
#pragma mark -换肤
- (void)applyTheme{
    [self setBackgroundColor:[UIColor colorWithPatternImage:[CNMultiThemeManager imageForKey:@"chatviewtool_floatview_bg.png"]]];
}

@end
